﻿Imports Tom0014
Imports Tom0014TableAdapters
Imports utileria
Partial Class Registro_CambiaClave
    Inherits System.Web.UI.Page
    Dim utils As New utileria

    Protected Sub Registro_CambiaClave_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        If Not IsPostBack Then
            utils.fillcatalogo()
            ddlHierro = utils.fillcatalogo(ddlHierro, "H")
        End If
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim iAnimal As New Integer
        ' Que el formulario haya sido cargado por primera vez
        If Not IsPostBack Then
            Try
                iAnimal = Request.QueryString("animal")
            Catch ex As Exception
                iAnimal = 0
            End Try
            ' si no hay animal seleccionado salgo del SUB
            If iAnimal = 0 Then Exit Sub

            Dim animal, crias As New GeneralDataTable
            Dim _animal As New GeneralTableAdapter
            ' Consulto a la base de datos para cargar los 
            ' datos a los controles
            animal = _animal.GetAnimalById(iAnimal)
            crias = _animal.GetCriasById(iAnimal)

            If animal.Count = 1 Then
                lblAnimal.Text = animal(0).ClaveSNLA & " " & animal(0).nombre
                lblNumero.Text = animal(0).numero.ToString
                lblLetra.Text = animal(0).letra
                lblAno.Text = animal(0).ano.ToString
                txtNumero.Text = animal(0).numero.ToString
                txtLetra.Text = animal(0).letra
                txtAno.Text = animal(0).ano.ToString
                lblNombre.Text = animal(0).nombre
                ddlHierro.SelectedValue = animal(0).hierro
                grvCrias.DataSource = crias
                grvCrias.DataBind()
            End If
        End If
    End Sub

    Protected Sub chkSelectAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim chkSeleccionado As CheckBox = grvCrias.HeaderRow.FindControl("chkSelectAll")
        For Each row In grvCrias.Rows
            Dim chk As CheckBox = row.FindControl("chkItemSelect")
            chk.Checked = chkSeleccionado.Checked
        Next

    End Sub

    Protected Sub btnAceptar2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAceptar2.Click
        Dim iAnimal As New Integer
        Dim animal As New GeneralDataTable
        Dim _animal As New GeneralTableAdapter

        Try
            iAnimal = Request.QueryString("animal")
        Catch ex As Exception
            iAnimal = 0
        End Try
        ' si no hay animal seleccionado salgo del SUB
        If iAnimal = 0 Then Exit Sub
        ' Busco el animal a modificar
        animal = _animal.GetAnimalById(iAnimal)
        Dim sSexo, sLetra, sHierro As String
        Dim iNumero, iAno As Integer
        ' Obtengo los cambios de clave
        sSexo = animal(0).sexo
        iNumero = CType(txtNumero.Text, Integer)
        sLetra = txtLetra.Text.ToUpper
        iAno = CType(txtAno.Text, Integer)
        sHierro = ddlHierro.SelectedValue
        ' Si existe el animal sigo con la ejecucion 
        If Not animal.Count = 1 Then Exit Sub
        animal(0).numero = iNumero
        animal(0).letra = sLetra
        animal(0).ano = iAno
        animal(0).hierro = sHierro
        Dim iActualizado As Integer
        ' Realizo la actualizacion de datos
        Try
            iActualizado = _animal.Update(animal)
        Catch ex As System.Data.SqlClient.SqlException
            ' Si falla envio el mensaje
            If ex.Class = 14 And ex.Number = 2601 Then
                pnActualizado.CssClass = "error"
                lblActualizado.Text = "No se puede cambiar, ya existe un animal asignado con las claves que intenta ingresar"
            End If
            Exit Sub
        Catch ex As Exception
            lblActualizado.Text = ex.Message
        End Try
        ' Si no se actualizo el registro salgo de la ejecucion
        If iActualizado = 0 Then Exit Sub

        ' En caso contrario reviso el GridView y actualizo las crias seleccionadas
        For Each row In grvCrias.Rows
            Dim reg As GridViewRow = row
            Dim chk As CheckBox = reg.FindControl("chkItemSelect")
            If chk.Checked Then
                reg.Cells.Item(3).Text = "mm"

            End If
        Next
    End Sub
End Class
